
//
// 120213 - AH - (Aura) Created.
//

package org.soekrat.engine.visitor;

/**
 * <p>
 *   Creates tuple.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class TupleFactory 
{
  private TupleFactory(){} // Utility pattern dictates private constructor.  

  /**
   * <p>
   *   Creates tuples of a given length.
   * </p>
   * @param input  {@link Input} to create tuple from.  
   * @param output Where to put created tuple.
   * @param n      Length of tuples to creates.
   * @param {{@code true} if tuples was created, {@code false} if none was and
   *        none will be of longer tuples.
   */
  public static boolean createTuples(Input input, Input output, int n)
  {
    boolean retVal=false;
    int start=0;
    while (createTuple(input, start, output, n))
    {
      start++;
      retVal=true;
    }
    return retVal;
  }
  
  /**
   * <p>
   *   Creates a tuple.
   * </p>
   * @param input  {@link Input} to create tuple from.
   * @param start  Where to start in {@code input}.
   * @param output Where to put created tuple.
   * @param n      Length of tuple.
   * @return {@code true} if tuple created else {@code false}. 
   */
  private static boolean createTuple(Input input, int start, Input output, 
    int n)
  {
    int to=start+n;
    
    int len=input.size();
    
    if (to>len) return false;
    
    StringBuilder sb=new StringBuilder();
    for (int i=start; i<to; i++)
    {
      String curr=input.getString(i);
      if (i>=start) sb.append(' ');
      sb.append(curr);
    }
    
    output.add(sb.toString());
    return true;
  }
  
  public static void main(String[] args) 
  {
    Input input=new Input("Arne", "is", "around", "here", "somewhere");
    Input output=new Input();
    createTuples(input, output, 3);
    
    
    System.out.println(output);  
  }
  
}
